package com.dongdongshop.service.impl;

import com.dongdongshop.domain.PageResult;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@DubboService
public class SpecificationServiceImpl implements SpecificationService {

    @Resource
    private TbSpecificationOptionMapper specificationOptionMapper;

    @Resource
    private TbSpecificationMapper specificationMapper;

    @Override
    public PageResult<TbSpecification> getInfo(Integer pageNum, Integer pageSize, TbSpecification specification) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbSpecification> list = specificationMapper.selectByName(specification);
        PageInfo<TbSpecification> page = new PageInfo<>(list);
        PageResult pr = new PageResult();
        pr.setPageNum(pageNum);
        pr.setPageSize(pageSize);
        pr.setPages(page.getPages());
        pr.setTotal(page.getTotal());
        pr.setRows(page.getList());
        return pr;
    }

    @Override
    public void toAdd(TbSpecification specification, List<TbSpecificationOption> options) {
        specificationMapper.insertSelective(specification);
        specificationOptionMapper.insertBySpecAndOption(specification.getId(),options);
    }

    @Override
    public List<TbSpecification> update(Long id) {
        List<TbSpecification> list = specificationMapper.selectById(id);
        return list;
    }

    @Override
    public void updateInfo(TbSpecification specification, List<TbSpecificationOption> options) {
        specificationMapper.updateByPrimaryKeySelective(specification);
        specificationOptionMapper.deleteByIdss(specification.getId());
        specificationOptionMapper.insertBySpecAndOption(specification.getId(),options);
    }

    @Override
    public void deleteInfo(Long[] ids) {
        specificationMapper.deleteInfo(ids);
        specificationOptionMapper.deleteByIds(ids);
    }

    @Override
    public List<TbSpecification> getInfo1() {
        return specificationMapper.selectByExample(null);
    }
}
